<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">账户信息</h4>
</div>
<div class="modal-body">
    <div class="form-horizontal row">
        <form id="form_data">
            <div id="div_error" class="col-sm-12"></div>
            <input type="hidden" id="id" name="id" value="${data.id}">


            <c:choose>
                <c:when test="${data.id==null}">
                    <div class="form-group col-sm-12">
                        <label class="control-label col-sm-4"><s>*</s>所&nbsp;&nbsp;属&nbsp;库：</label>
                        <div class="col-sm-5">
                            <select class="form-control" name="subjectType" id="subjectType">
                                <option value="2" selected>员工库</option>
                                <option value="1" data-varg>企业库</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group col-sm-12">
                        <label id="label_subjectName" class="control-label col-sm-4"><s>*</s>姓　　名：</label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" name="subjectName">
                        </div>
                    </div>

                    <div class="form-group col-sm-12">
                        <label id="label_loginName"
                               class="control-label col-sm-4"><s>*</s>登&nbsp;&nbsp;录&nbsp;名：</label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" name="loginName" id="loginName">
                        </div>
                    </div>

                    <div class="form-group col-sm-12">
                        <label class="control-label col-sm-4"><s>*</s>密　　码：</label>
                        <div class="col-sm-5">
                            <input type="password" class="form-control" name="password">
                        </div>
                    </div>

                    <div class="form-group col-sm-12">
                        <label class="control-label col-sm-4"><s>*</s>确认密码：</label>
                        <div class="col-sm-5">
                            <input type="password" class="form-control" name="password1">
                        </div>
                    </div>
                </c:when>
                <c:otherwise>
                    <div class="form-group col-sm-12">
                        <label class="control-label col-sm-4">登&nbsp;&nbsp;录&nbsp;名：</label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" disabled value="${data.loginName}">
                        </div>
                    </div>
                </c:otherwise>
            </c:choose>

            <input type="hidden" name="live" value="1">
            <%--<div class="form-group col-sm-12">--%>
            <%--<label class="col-sm-4 control-label"><s>*</s>状　　态：</label>--%>
            <%--<div class="col-sm-5">--%>
            <%--<select class="form-control" name="live">--%>
            <%--<option value="">&nbsp;</option>--%>
            <%--<option--%>
            <%--<c:if test="${data.live==0}">selected</c:if> value="0">停用--%>
            <%--</option>--%>
            <%--<option--%>
            <%--<c:if test="${data.live==1}">selected</c:if> value="1">启用--%>
            <%--</option>--%>
            <%--</select>--%>
            <%--</div>--%>
            <%--</div>--%>

            <div class="form-group col-sm-12">
                <label class="col-sm-4 control-label"><s>*</s>角　　色：</label>
                <div class="col-sm-5">
                    <select class="form-control" id="roleId" name="roleId">
                        <option value="">&nbsp;</option>
                        <c:forEach items="${role}" var="i">
                            <option
                                    <c:if test="${data.roleId==i.id}">selected</c:if> value="${i.id}">${i.name}
                            </option>
                        </c:forEach>
                    </select>
                </div>
            </div>

            <div class="form-group col-sm-12">
                <label class="col-sm-4 control-label"><s>*</s>权　　限：</label>
                <div class="col-sm-5">

                    <ul id="treeDemo1" class="ztree  form-control-free"></ul>

                </div>
            </div>


            <div class="form-group col-sm-12">
                <div class="col-sm-4 col-sm-offset-4">
                    <button type="submit"
                            class="btn btn-default btn-success"><span
                            class="glyphicon glyphicon-ok"></span>&nbsp;确&nbsp;定&nbsp;
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>

    $("#subjectType").change(function () {
        var value = $(this).find("option:selected").val();
        if (value == 1) {
            $("#label_subjectName").html("<s>*</s>企业名称：");
            $("#label_loginName").html("<s>*</s>组织机构代码：");
            $("#loginName").attr("placeholder", "可作为登录名，登录本系统");
        } else {
            $("#label_subjectName").html("<s>*</s>姓　　名：");
            $("#label_loginName").html("<s>*</s>登&nbsp;&nbsp;录&nbsp;名：");
            $("#loginName").attr("placeholder", "");
        }
    });

    $(function () {

        var setting = {
            check: {
                enable: true,
                checkboxType: {"Y": "", "N": ""}
            },
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            async: {
                enable: true,
                url: "upms/permission/selectByAccountIdWithAll",
                otherParam: {"accountId": $("#id").val()}
            }
        };
        $.fn.zTree.init($("#treeDemo1"), setting);
        $("#roleId").change(function () {
            $.fn.zTree.destroy("treeDemo1");
            $.extend(setting, {
                async: {
                    enable: true,
                    url: "upms/permission/selectAllActivePermissionByRoleId",
                    otherParam: {"roleId": $("#roleId").val()}
                }
            });
            $.fn.zTree.init($("#treeDemo1"), setting);
        });


    });
</script>

<script>
    <%--bootstrapValidator--%>
    var $form_data = $("#form_data");
    $form_data.bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            <c:if test="${data.id==null}">
            subjectName: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }

                }
            },
            loginName: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }

                }
            }, password: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }, identical: {
                        field: 'password1',
                        message: '密码不一致'
                    }

                }
            }, password1: {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }, identical: {
                        field: 'password',
                        message: '密码不一致'
                    }

                }
            },
            </c:if>
            'roleId': {
                validators: {
                    notEmpty: {
                        message: '不能为空！'
                    }

                }
            }

        }
    }).on('success.form.bv', function (e) {
        e.preventDefault();

        var treeObj = $.fn.zTree.getZTreeObj("treeDemo1");
        var nodes = treeObj.getCheckedNodes(true);
        var ids = new Array();
        for (var i = 0; i < nodes.length; i++) {
            ids[i] = nodes[i].id;
        }

        if (ids.length == 0) {
            $("#div_error").html($.sysPlugin.createAlert("权限不能为空"));
            return;
        }

        var data = $(e.target).serializeObject();
        data['permission'] = ids;

        $.sysPlugin.superAjax({
            data: data,
            url: '${url}',
            operateType: '${operateType}',
            showSuccessAlert: true,
            showWarnAlert: false,
            successCall: function (data) {
                $.sysPlugin.successCallBack(data, {
                    modal: $("#modal_ajax"),
                    table: $("#table_data")
                })
            },
            errorCall: function (data) {
                $.sysPlugin.errorCallBack(data, {
                    form: $form_data
                })
            }
        })

    });


    $.sysPlugin.initSimpleSelect2();
</script>
